package com.bitcola.exchange.push.rest;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.alicp.jetcache.anno.CachePenetrationProtect;
import com.alicp.jetcache.anno.CacheRefresh;
import com.alicp.jetcache.anno.CacheType;
import com.alicp.jetcache.anno.Cached;
import com.bitcola.exchange.push.http.OKHttpUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RequestMapping(value = "support/v2")
@RestController
public class SupportsController {
    @Value("${support.zh}")
    String zhUrl;

    @Value("${support.en}")
    String enUrl;
    @Value("${support.base_zh}")
    String base_cn;

    @Value("${support.base_en}")
    String base_en;

    @RequestMapping(value = "last")
    @Cached(cacheType = CacheType.LOCAL,expire = 70)
    @CacheRefresh(refresh = 60, stopRefreshAfterLastAccess = 43600, timeUnit = TimeUnit.SECONDS)
    @CachePenetrationProtect
    public JSONObject last(Integer page, Integer size, String language) {
        String url;
        if(language.equals("zh")) {
            url = String.format(zhUrl, page, size);
        }else{
            url = String.format(enUrl, page, size);
        }
        String content = OKHttpUtil.httpGet(url);
        System.out.println(url);
        return JSONObject.parseObject(content);

    }


    @RequestMapping(value = "page")
    @Cached(cacheType = CacheType.LOCAL)
    @CacheRefresh(refresh = 180, stopRefreshAfterLastAccess = 43600, timeUnit = TimeUnit.SECONDS)
    @CachePenetrationProtect
    public String page(String url,String language) {

        String baseUrl;
        if(language.equals("zh")) {
            baseUrl = base_cn;
        }else{
            baseUrl =base_en;
        }
        String content=null;
        baseUrl=baseUrl+url;
        try {
            content = HttpUtil.get(baseUrl);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return content;
    }
}
